package com.example.edu.web;

import com.example.edu.domain.Dependency;
import com.example.edu.domain.Result;
import com.example.edu.domain.dto.DependencyDTO;
import com.example.edu.domain.query.DependencyQuery;
import com.example.edu.domain.vo.DependencyVO;
import com.example.edu.domain.vo.PageVO;
import com.example.edu.service.DependencyService;
import com.example.edu.validate.AddGroup;
import com.example.edu.validate.PageGroup;
import com.example.edu.validate.UpdateGroup;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author yanzhao
 * @date 2022/1/13 11:00
 * @since 1.0.0
 */
@RestController
@RequestMapping("api/v1.0/dependency")
public class DependencyController {

    @Resource
    private DependencyService dependencyService;

    @GetMapping("page")
    public Object getPage(@Validated(PageGroup.class) DependencyQuery query) {
        PageVO<DependencyVO> pageVO = dependencyService.getPage(query, new ArrayList<>());
        return Result.success(pageVO);
    }

    @PostMapping()
    public Object insert(@Validated(AddGroup.class) @RequestBody DependencyDTO depDTO) {

        Dependency starterDep = new Dependency();
        starterDep.setGroupId(depDTO.getGroupId());
        starterDep.setArtifactId(depDTO.getArtifactId());
        starterDep.setVersion(depDTO.getVersion());
        starterDep.setScope(depDTO.getScope());
        starterDep.setOptional(depDTO.getOptional());
        dependencyService.save(starterDep);
        return Result.success(starterDep.getId());
    }

    @PutMapping
    public Object update(@Validated(UpdateGroup.class) @RequestBody DependencyDTO depDTO) {

        Dependency starterDep = new Dependency();
        starterDep.setId(depDTO.getId());
        starterDep.setGroupId(depDTO.getGroupId());
        starterDep.setArtifactId(depDTO.getArtifactId());
        starterDep.setVersion(depDTO.getVersion());
        starterDep.setScope(depDTO.getScope());
        starterDep.setOptional(depDTO.getOptional());
        dependencyService.updateById(starterDep);
        return Result.success(starterDep.getId());
    }

    @DeleteMapping("{id}")
    public Object delete(@PathVariable Long id) {
        Dependency starterDep = dependencyService.getById(id);
        if (starterDep == null) {
            return Result.error("数据不存在", Result.StatusCode.PARAM_MISSING);
        }

        return Result.success(dependencyService.removeById(id));
    }

    @GetMapping("{id}")
    public Object get(@PathVariable Long id) {
        Dependency dependency = dependencyService.getById(id);
        if (dependency != null) {
            DependencyVO depVO = new DependencyVO();
            depVO.setId(dependency.getId() + "");
            depVO.setGroupId(dependency.getGroupId());
            depVO.setArtifactId(dependency.getArtifactId());
            depVO.setVersion(dependency.getVersion());
            depVO.setScope(dependency.getScope());
            depVO.setOptional(dependency.getOptional());
            depVO.setGmtCreate(dependency.getGmtCreate());
            depVO.setGmtModified(dependency.getGmtModified());
            return Result.success(depVO);
        }
        return Result.error("没有找到数据", Result.StatusCode.PARAM_MISSING);
    }
}
